﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace FranceTriple.Dal
{
    public class PictureManager
    {
        public static void SaveOnePictureToDisk(String SourcePictureUrl, String TargetFilePathAndName, Boolean IsThumb)
        {
            HttpWebRequest request;
            HttpWebResponse response;

            System.Drawing.Image img;

            if (!Directory.Exists(TargetFilePathAndName))
                System.IO.Directory.CreateDirectory(TargetFilePathAndName.Substring(0, TargetFilePathAndName.LastIndexOf("\\")));

            request = (HttpWebRequest)HttpWebRequest.Create(SourcePictureUrl);
            request.Timeout = 5000;
            request.ReadWriteTimeout = 20000;
            response = (HttpWebResponse)request.GetResponse();
            img = System.Drawing.Image.FromStream(response.GetResponseStream());

            if (IsThumb)
                img = img.GetThumbnailImage(200, (img.Height * 200 / img.Width), null, IntPtr.Zero);

            switch (SourcePictureUrl.Substring(SourcePictureUrl.Length - 3).ToLower())
            {
                case "jpg":
                    img.Save(TargetFilePathAndName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "png":
                    img.Save(TargetFilePathAndName, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                case "gif":
                    img.Save(TargetFilePathAndName, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                default:
                    throw new ApplicationException("UnknownPictureFormat".ToLocalizedString() + " : " + SourcePictureUrl);
            }
        }

        public static void PictureToThumb(String SourcePath)
        {
            System.Drawing.Image img;
            String TargetFile=String.Empty;

            if (!Directory.Exists(SourcePath))
                throw new ApplicationException("PathDontExists".ToLocalizedString() + " : " + SourcePath);

            IEnumerable<String> pictures = Directory.EnumerateFiles(SourcePath, "*.*", SearchOption.AllDirectories);
            if (pictures == null)
                return;

            foreach (String pic in pictures)
            {
                if (pic.Contains("index.html") || pic.Contains("thumb_")) continue;

                TargetFile = Path.Combine(Path.GetDirectoryName(pic), "thumb_" +  Path.GetFileName(pic));
                img = System.Drawing.Image.FromFile(pic);
                img = img.GetThumbnailImage(200, (img.Height * 200 / img.Width), null, IntPtr.Zero);

                switch (Path.GetExtension(pic).ToLower())
                {
                    case ".jpg":
                        img.Save(TargetFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case ".png":
                        img.Save(TargetFile, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    case ".gif":
                        img.Save(TargetFile, System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    default:
                        throw new ApplicationException("UnknownPictureFormat".ToLocalizedString() + " : " + pic);
                }
            }
        }
        public static bool RemoteFileExists_HttpWebRequest(String url)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "HEAD";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                return (response.StatusCode == HttpStatusCode.OK);
            }
            catch
            {
                return false;
            }
        }

        public static bool RemoteFileExists_WebClient(String url)
        {
            bool result = false;
            using (WebClient client = new WebClient())
            {
                try
                {
                    Stream stream = client.OpenRead(url);
                    if (stream != null)
                    {
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
                catch
                {
                    //Any exception will returns false.  
                    result = false;
                }
            }
            return result;
        }
    }
}
